﻿using System.Management;

namespace Browser
{
    public class HardwareIdentifier
    {
        public static string GetHardwareIdentifier()
        {
            return string.Format("{0}@{1}", GetCpuInfo(), GetMacAddress()).Replace(" ", string.Empty);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="maxLen">指定返回字符串的长度，如果不足这个长度则返回实际长度</param>
        /// <returns></returns>
        public static string GetHardwareIdentifier(int maxLen)
        {
            var id = GetHardwareIdentifier();
            return id.Length > 50 ? id.Substring(0, 50) : id;
        }
        private static string GetMachineName()
        {
            string name = "MachineName-unknown";
            try
            {

                return System.Environment.MachineName;
            }
            catch
            {
                return name;
            }
        }

        /// <summary>
        /// 获取系统CPU ID，失败时返回“cpu-unknown”
        /// </summary>
        /// <returns></returns>
        private static string GetCpuInfo()
        {
            string cpu = "cpu-unknown";
            try
            {
                using (var mobj = new ManagementClass("Win32_Processor"))
                {
                    if (mobj != null)
                    {
                        var moc = mobj.GetInstances();
                        if (moc.Count > 0)
                        {
                            var mo = moc.GetEnumerator();
                            mo.MoveNext();
                            cpu = mo.Current.Properties["ProcessorId"].Value.ToString();
                        }
                    }
                }
                return cpu;
            }
            catch
            {
                return cpu;
            }
        }
        /// <summary>
        /// 获取系统硬盘信息信息，失败时返回“hdd-unknown”
        /// </summary>
        /// <returns></returns>
        private static string GetHddInfo()
        {
            string hdd = "hdd-unknown";
            try
            {
                using (var mobj = new ManagementClass("Win32_PhysicalMedia"))
                {
                    if (mobj != null)
                    {
                        var moc = mobj.GetInstances();
                        if (moc.Count > 0)
                        {
                            var mo = moc.GetEnumerator();
                            mo.MoveNext();
                            hdd = mo.Current.Properties["SerialNumber"].Value.ToString();
                        }
                    }
                }
                return hdd;
            }
            catch
            {
                return hdd;
            }
        }
        /// <summary>
        /// 获取系统MAC地址，失败时返回“mac-unknown”
        /// </summary>
        /// <returns></returns>
        private static string GetMacAddress()
        {
            string mac = "mac-unknown";
            try
            {
                using (var mc = new ManagementClass("Win32_NetworkAdapterConfiguration"))
                {
                    ManagementObjectCollection moc = mc.GetInstances();
                    foreach (ManagementObject mo in moc)
                    {
                        if ((bool)mo["IPEnabled"] == true)
                        {
                            mac = mo["MacAddress"].ToString();
                            break;
                        }
                    }
                    moc = null;
                }
                return mac;
            }
            catch
            {
                return mac;
            }
        }
    }
}
